package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName DepartmentServiceImpl
 * @Description: 类描述
 * @Author: admin
 * @CreateDate: 2024/6/4 17:30
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/6/4 17:30
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;


    /**
     * 保存科室信息
     * @param paramsMap
     */
    @Override
    public void save(Map<String, Object> paramsMap) {
        //将map转为json字符串
        String paramsMapStr = JSONObject.toJSONString(paramsMap);
        //将json字符串转为Department对象
        Department department = JSONObject.parseObject(paramsMapStr, Department.class);
        //判断科室是否存在
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if(departmentExist!= null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {
            department.setUpdateTime(new Date());
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
        //保存到数据库
    }


    /**
     * 获取科室列表
     * @param departmentQueryVo 查询条件
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(DepartmentQueryVo departmentQueryVo, Integer page, Integer limit) {
        //设置页面以及每页显示的条数，0才是第一页
        Pageable pageable = PageRequest.of(page-1, limit);

        Department department = new Department();
        //将departmentQueryVo对象转为department对象
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        //构建查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                //设置为模糊查询
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                //忽略大小写
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);

        return all;
    }

    /**
     * 根据hoscode和depcode删除科室信息
     * @param hoscode
     * @param deptcode
     */
    @Override
    public void remove(String hoscode, String deptcode) {
        //根据医院编号和科室编号查询科室信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, deptcode);
        //如果存在，则删除
        if(department!= null){
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 获取科室树
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> getDeptTree(String hoscode) {
        //创建list集合，用于存放部门树
        List<DepartmentVo> result = new ArrayList<>();
        //查询所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        List<Department> all = departmentRepository.findAll(example);
        //通过stream流进行分组，根据大科室进行分组，获取每个大科室的子科室
        Map<String, List<Department>> departmentMap =
                all.stream()
                .collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String,List<Department>> entry : departmentMap.entrySet()) {
            //获取大科室的名称
            String BigCode = entry.getKey();
            //获取大科室的子科室
            List<Department> department1List = entry.getValue();

            DepartmentVo departmentVo1 = new DepartmentVo();
            //获取大科室的编号
            departmentVo1.setDepcode(BigCode);
            //获取大科室的名称
            departmentVo1.setDepname(department1List.get(0).getDepname());

            //封装子科室信息
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : department1List) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                children.add(departmentVo2);
            }
            //把小科室添加到大科室的子科室中，完成树形结构
            departmentVo1.setChildren(children);
            //把大科室添加到树形结构中
            result.add(departmentVo1);
        }
        return result;
    }

    /**
     * 根据hoscode和depcode获取科室名称
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
